package com.sxhalo.fsms.controller.api;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.SysDicType;
import com.sxhalo.fsms.exception.BaseEnumError;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.ISysDicTypeService;
import com.sxhalo.fsms.util.TreeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxhalo.fsms.util.ApiResult;

@Controller
@RequestMapping("/api")
public class SysDicTypeController extends BaseController {

	@Autowired
	private ISysDicTypeService typeService;
	
	@ResponseBody
	@RequestMapping(value = "/dicType", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDicType(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "1=1";
		String order = "";
		if(!isBlank(req.getParameter("dicTypeName"))) {
			where += " and dic_type_name like '%" + req.getParameter("dicTypeName") + "%'";
		}
		if (!isBlank(req.getParameter("description"))) {
			where += " and description like '%" + req.getParameter("description") + "%'";
		}
		return findList(current, pageSize, where, order, typeService);
	}
	@ResponseBody
	@RequestMapping(value = "/dicttype/tree", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findTreedata(HttpServletRequest req) {
		List<SysDicType> types =	typeService.findWhereList("delete_flag=0","");
		List<TreeData> treeData = new ArrayList<>();
		for (SysDicType type : types) {
			TreeData item = new TreeData();
			item.setTitle(type.getTypeName());
			item.setKey(type.getTypeId().toString());
			item.setValue(type.getTypeId().toString());

			treeData.add(item);
		}
		return ApiResult.retOK(req, treeData);
	}



	@ResponseBody
	@SysControllerLog(operationName = "新增字典类型", modelName = "字典类型管理")
	@RequestMapping(value = "/dictype", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDicType(@RequestBody SysDicType record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setCreateTime(nowtime);
		record.setDeleteFlag(0);
		typeService.insertSelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "更新字典类型", modelName = "字典类型管理")
	@RequestMapping(value = "/dictype", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDicType(@RequestBody  SysDicType record, String method, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		typeService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@RequestMapping(value = "/dictype/validTypeName", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult validCode(String typeName, HttpServletRequest req) {
		List<SysDicType> records = typeService.findWhereList("type_name='"+typeName+"'","");
		if (records == null||records.size()==0) {
			return ApiResult.retOK(req);
		}
		return ApiResult.retFail(BaseEnumError.数据重复);
	}


}
